using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LiveStock.dataAccess;

namespace LiveStock
{
    public partial class LoginForm : FadeForm
    {
        Boolean firstTimeAdmin = false; //If a first time user has been detected this will change to True.
      
        //constructor
        public LoginForm()
        {            
            InitializeComponent();
        }         

        
        #region buttons

        private void Login_btnNewUser_Click(object sender, EventArgs e)
        {
            //Create a current user, default for firstTimeAdmin is false, unless firsTimeAdmin is detected.
            //create the new ManageUser form passing the current user, then show the form.
            CurrentUser cur = new CurrentUser(firstTimeAdmin);
            ManageUser man = new ManageUser(cur);
            
            //Show the form as a dialog (focus cannot be taken away from the new form)
            //if the form returns a result of ok, then dispose the form and hide the login form.
            man.ShowDialog();
            DialogResult d =  man.DialogResult;
            if (man.DialogResult == DialogResult.OK)
            {
                man.Dispose();
                //this.Focus();
                this.Hide();
            }
            
        }

        private void Login_btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                Users.UsersDataTable udt = UsersData.UserLogin(Login_txtUserName.Text, Login_txtPassword.Text);
                if (udt.Rows.Count > 0)
                {
                    CurrentUser cur = new CurrentUser(udt[0].UserID, udt[0].UserName, udt[0].Password, udt[0].FullName, udt[0].UserType);

                    //Launch the MainForm sending the current user object.
                    MainForm bob = new MainForm(cur);
                    bob.Show();
                    this.Hide();
                }
                else
                    MessageBox.Show("Incorrect User Name or Password.", "Login Incorrect", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch
            {
                DialogResult r = MessageBox.Show("You MUST have atleast SQL Server Express running/installed to use LiveStock\nLiveStock will now Exit.", "SQL?", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }

            }

        private void Login_picHelp_Click(object sender, EventArgs e)
        {
            Help.ShowHelp(this, "LiveStock.chm");
        }

        #endregion          
        
        #region Form Handling and Other Functions

        private void Login_btnNewUser_MouseHover(object sender, EventArgs e)
        {
            if (firstTimeAdmin)
            {
                //tipFirstTimeUser.Active = false;
                //tipFirstTimeUser.Show("This is your first time using LiveStock, Click 'New User' to set up the Admin account.", this.Login_btnNewUser);
                Point point = new Point(this.Login_btnNewUser.Left + 40,this.Login_btnNewUser.Top + 40);
                tipFirstTimeUser.Active = false;
                tipFirstTimeUser.Show("This is your first time using LiveStock,\n Click 'New User' to set up the Admin account.",this,point);
                tipFirstTimeUser.Active = true;
                tipFirstTimeUser.Show("This is your first time using LiveStock,\n Click 'New User' to set up the Admin account.", this, point); 
            
            }
        }

        private void Login_txtPassword_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Login_btnLogin.Focus();
                Login_btnLogin.PerformClick();
                e.Handled = true;
            }
        }

        private void setGUI(Boolean enabled)
        {            
                Login_txtUserName.Enabled = enabled;
                Login_txtPassword.Enabled = enabled;
                Login_btnLogin.Enabled = enabled;
           
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            //CHECK TO SEE IF ADMIN EXISTS (FIRST TIME LOGIN, FIRST USER IS TO BECOME ADMIN
            Users.UsersDataTable udt = UsersData.AdminCheck();
            if (udt.Rows.Count == 0)
            {
                firstTimeAdmin = true;
                setGUI(false);
            }
        }

        #endregion



    }
}